# -*- coding: utf-8 -*-

THRESHOLD = 0.000001


def singleton(cls):
    _instance = {}

    def _singleton(*args, **kwargs):
        if cls not in _instance:
            _instance[cls] = cls(*args, **kwargs)
        return _instance[cls]

    return _singleton


@singleton
class Normal:
    def __init__(self):
        self.unit = 0.0
        self.base = -5000000
        self.width = 10000000
        self.cad_width = 100000
        self.base_x = 0.0
        self.base_y = 0.0
        self.left = 0.0
        self.right = 0.0
        self.bottom = 0.0
        self.top = 0.0

    def init(self, x, y):
        self.base_x = x - self.cad_width / 2
        self.base_y = y - self.cad_width / 2
        self.left = x - self.cad_width / 2
        self.right = x + self.cad_width / 2
        self.bottom = y - self.cad_width / 2
        self.top = y + self.cad_width / 2
        self.unit = self.width / self.cad_width

    def nx(self, x):
        return self.base + (x - self.base_x) * self.unit

    def ny(self, y):
        return self.base + (y - self.base_y) * self.unit

    def dx(self, x):
        return self.base_x + (x - self.base_x) / self.unit

    def dy(self, y):
        return self.base_y + (y - self.base_y) / self.unit
